package cn.semcorp.zhuhaizj.listener;

import cn.semcorp.zhuhaizj.common.Common;
import cn.semcorp.zhuhaizj.exception.ServiceException;
import cn.semcorp.zhuhaizj.pojo.dto.PulpingDateDTO;
import cn.semcorp.zhuhaizj.restful.ResponseCode;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.List;
import java.util.regex.Pattern;


/**
 * 制浆领料数据读取分析器
 */
public class ReadPulpingDateListener extends AnalysisEventListener<PulpingDateDTO> {


    public List<PulpingDateDTO> pulpingDateDTOList;
    public ReadPulpingDateListener(List<PulpingDateDTO> list){
        this.pulpingDateDTOList = list;
    }
    @Override
    public void invoke(PulpingDateDTO pulpingDateDTO, AnalysisContext analysisContext) {
        Pattern pattern = Pattern.compile(Common.regex);

        // 日期格式判断
        if (!pattern.matcher(pulpingDateDTO.getDate()).matches()) {
            throw new ServiceException(ResponseCode.BAD_REQUEST, "日期格式错误,请提供纯数字格式!");
        }
        pulpingDateDTOList.add(pulpingDateDTO);
    }

    /**
     * 读完aop
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    }

    /**
     * 获取读对象集合
     */
    public List<PulpingDateDTO> getPulpingDateDTOList(){
        return pulpingDateDTOList;
    }
}
